#include <stdio.h>
#include <memory.h>

#define MAX_N 256

typedef unsigned int bitset_elem_t;
#define BPE (sizeof(bitset_elem_t)*8) /* bits per element */
typedef bitset_elem_t bitset_t[(MAX_N+BPE-1)/BPE]; /* MAX_N bits */

#define BITSET_TEST(bs,index) ((bs[(index)/BPE] & (1 << ((index) % BPE))) != 0)
#define BITSET_SET(bs,index)  ((bs[(index)/BPE] |= (1 << ((index) % BPE))))
#define BITSET_CLEAR(bs,size) memset(bs,0,((size)+BPE-1)/BPE*sizeof(bitset_elem_t))
#define BITSET_SETALL(dst,src,size) do { \
    size_t i; \
    for (i = 0; i < ((size)+BPE-1)/BPE; i++) \
        dst[i] |= src[i]; \
    } while (0)

static bitset_t child[MAX_N]; /* bitset of children, including indirect */
static bitset_t owner[MAX_N]; /* bitset of parents, including indirect */

/* Note: consider the test case: B buy C, A buy B, C buy A */
int main()
{
    int N, T, t;
    for (t = 1; scanf("%d %d", &N, &T) == 2 && !(N == 0 && T == 0); t++)
    {
        int i, j, reject = 0;
        for (i = 0; i < N; i++)
        {
            BITSET_CLEAR(owner[i], N);
            BITSET_CLEAR(child[i], N);
            BITSET_SET(owner[i], i);
            BITSET_SET(child[i], i);
        }

        for (j = 0; j < T; j++)
        {
            int a, b; /* a to buy b */
            int aa, bb;
            scanf("%d %d", &a, &b);
            --a;
            --b; /* make them zero based */

            /* Cannot buy if b is an owner of a. Note that since any company
             * owns itself by initialization, this also covers the case where
             * a company buys itself. */
            if (BITSET_TEST(owner[a], b))
            {
                reject++;
                continue;
            }

            /* If a is already an owner of b, do nothing. */
            if (BITSET_TEST(owner[b], a))
                continue;
            
            /* For each owner of a, mark all children of b as its children. */
            for (aa = 0; aa < N; aa++)
            {
                if (BITSET_TEST(owner[a], aa) && !BITSET_TEST(child[aa], b))
                    BITSET_SETALL(child[aa], child[b], N);
            }

            /* For each child of b, mark all owners of a as its owners. */
            for (bb = 0; bb < N; bb++)
            {
                if (BITSET_TEST(child[b], bb) && !BITSET_TEST(owner[bb], a))
                    BITSET_SETALL(owner[bb], owner[a], N);
            }
        }

        printf("%d. %d\n", t, reject);
    }
    return 0;
}
